import requests
import json
import pymongo

# mongo连接
mongo_conn = pymongo.MongoClient()

for page in range(1, 192 + 1):
    # 模拟请求
    url = "https://api.bilibili.com/pgc/season/index/result"
    params = {'st': '1', 'order': '3', 'season_version': '-1', 'spoken_language_type': '-1', 'area': '-1',
              'is_finish': '-1', 'copyright': '-1', 'season_status': '-1', 'season_month': '-1', 'year': '-1',
              'style_id': '-1', 'sort': '0', 'page': page, 'season_type': '1', 'pagesize': '20', 'type': '1'}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(url, params=params,headers=headers)
    result = response.content.decode()
    print(response.status_code)
    print(result)

    # 数据解析 json反序列化解析需要的数据字段
    result = json.loads(result)
    # 批量插入 参数要求为list套dict
    mongo_conn.bilibili.data.insert_many(result.get('data').get('list'))
    # for item in result.get('data').get('list'):
    #     order = item.get('order')
    #     title = item.get('title')
    #     print(page,order, title)
    #     # 保存到mongo 数据存储 单条插入  参数要求为dicts
    #     mongo_conn.bilibili.data.insert_one({'order': order, 'title': title})